<?php
require ('includes/config.php');
$page_title = 'Events';
include ('includes/header.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	$keyword = $_POST['keyword'];
$searchfield = $_POST['searchfield'];	
	}
  
  
  
   
?>
 <!-- fluid body container -->

<div class="container-fluid">
<!--fluid sidebar 3 columns   -->
	<div class="span3">
		<div class="well sidebar-nav">
<?php
require ('includes/side_nav_events.php');
?>
         </div>
    </div>
   <!---end of sidebar 3 columns  -->
  
  <div class="span9">
  		<div class="hero-unit">
        
        <h2>Events list</h2>
        <p>Click on the row header to sort the results</p>
        
   <form class = "form-search"id="searchform" name="searchform" method="post" action="Events.php">
<label><b>Search:</b></label>
<input type="hidden" value="search" name="submitted">
<input type="text" title="Enter search text" size="12" name="keyword" value="" alt="search">
<select name = "searchfield">
<option value = "text">Event Name</option>
<option value="start_date">Event Date</option>
<option value="type">Event Type</option>
</select>
<input class = "btn-large btn-primary" type="submit" title="Click to search our site for products etc" value="GO" name="goButton" style="margin:0px;padding:0px;">
</form>
 <?php require(MYSQL);  //open database        


//setting value for number results per page
	$display = 10;

//chking if the number of pages has been set	
	if (isset($_GET['p']) && is_numeric($_GET['p'])){
		
		$pages = $_GET['p'];
			
		}else{
	//count number of records
	$q = "SELECT COUNT(ev_id) FROM events";	
	$r = @mysqli_query ($dbc, $q);
	$row = @mysqli_fetch_array ($r, MYSQLI_NUM);
	$records = $row[0];
	// Calculate the number of pages...
	if ($records > $display) { // More than 1 page.
		$pages = ceil ($records/$display);
		} else {
		$pages = 1;
	}
} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
	$start = $_GET['s'];
} else {
	$start = 0;
}

// Determine the sort...
// Default is by registration date.
$sort = (isset($_GET['sort'])) ? $_GET['sort'] : 'n';

// Determine the sorting order:
switch ($sort) {
	case 't':
		$order_by = 'type';
		break;
	case 'sd':
		$order_by = 'start_date';
		break;
	default:
		$order_by = 'text';
		$sort = 'n';
		break;
}
	

if (isset($keyword)){		
            //Search for comments which contain a keyword
			$q=" SELECT `ev_id`,`text`,`start_date`,`type` FROM `events`  WHERE $searchfield LIKE '%$keyword%' ORDER BY start_date";
$r = mysqli_query ($dbc, $q);
 

            echo "<p>Search for keyword - $keyword</p>";

  
}
else{
   $q="SELECT `ev_id`,`text`,`start_date`,`type` FROM `events` ORDER BY  $order_by  ASC LIMIT $start, $display";//to print out all events
}
   $r = mysqli_query ($dbc, $q);
   echo '<table border="0" width="95%" cellspacing="3" cellpadding="3" align="center">
	<tr>
		<td align="left" width="5%"><b>ID</b></td>
		<td align="left" width="25%"><a href="Events.php?sort=sd">Event date</a></td>
		<td align="left" width="35%"><a href="Events.php?sort=n">Name</a></td>
		<td align="left" width="15%"><a href="Events.php?sort=t">Category</a></td>
		<td align="left" width="10%"></td>
		<td align="left" width="10%"></td>
	</tr>';
   

   
   while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
	   
	  
   echo "<tr>
		<td align=\"left\"><a href=\"index.php\"> {$row['ev_id']}</td>
		<td align=\"left\">{$row['start_date']}</td>
		<td align=\"left\">{$row['text']}</td>
		<td align=\"left\">{$row['type']}</td>
		<td align=\"left\"><a href=\"delete.php?eid={$row['ev_id']}\">Delete</td>
		<td align=\"left\"><a href=\"ammend.php?eid={$row['ev_id']}\">Amend</td>
	</tr>\n";
		

} // End of while loop.

echo '</table>';
mysqli_free_result ($r);
mysqli_close($dbc);


// Make the links to other pages, if necessary.
if ($pages > 1) {

	echo '<br /><p>';
	$current_page = ($start/$display) + 1;

	// If it's not the first page, make a Previous button:
	if ($current_page != 1) {
		echo '<a href="Events.php?s=' . ($start - $display) . '&p=' . $pages . '">Previous</a> ';
	}
	
	// Make all the numbered pages:
	for ($i = 1; $i <= $pages; $i++) {
		if ($i != $current_page) {
			echo '<a href="Events.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
		} else {
			echo $i . ' ';
		}
	} // End of FOR loop.
	
	// If it's not the last page, make a Next button:
	if ($current_page != $pages) {
		echo '<a href="Events.php?s=' . ($start + $display) . '&p=' . $pages .  '">Next</a>';
	}
	
	echo '</p>'; // Close the paragraph.
	
} // End of links section.
	


   ?>
   		 </div>
  <!--end of hero-unit -->
  <!--start of events list -->
  		  <div class="row-fluid">This is where the events list will go
 		   </div>
    </div>
  <!--end of events list -->
<!-- start of left bar to do list -->
 	
    </div>
    <!-- end of to do list-->
    </div>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="js/bootstrap.min.js"></script>
	<script src="js/bootstrap.js"></script>
  </body>
</html>
<?php ob_flush(); ?>
